package org.ceteca.explica.core.management;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.CountryOT;
import org.ceteca.explica.core.common.CurrencyOT;
import org.ceteca.explica.core.common.DocumentTotal;
import org.ceteca.explica.core.common.StateOT;
import org.ceteca.explica.core.partners.PartnerOT;
import org.ceteca.explica.core.partners.PaymentMethodOT;
import org.ceteca.explica.core.partners.VendorOT;
import org.ceteca.explica.core.warehouse.WarehouseOT;

public class DocumentOT extends BaseVO implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private long idDocument;
	private DocumentTypeOT documentType;
	private Date dateDocument = new Date();;
	private Integer version = new Integer(0);
	private String serialNumberPrefix;
	private long serialNumberValue;
	private PartnerOT partner;
	private VendorOT vendor;
	private CurrencyOT currency;
	private PaymentMethodOT paymentMethod;
	private Date lastUpdate = new Date();
	private int vatRegime;
	private BigDecimal promptPaymentDiscount, fixedDiscount;
	private String name, brandName, idNumber, address_billing,
			address_shipping;
	private String postalCode_billing, postalCode_shipping, city_billing,
			city_shipping;
	private StateOT state_billing, state_shipping;
	private CountryOT country_billing, country_shipping;

	private SerialNumberOT serial;
	
	private DocumentTotal total;
	private WarehouseOT warehouse;
	
	
	
	
	public WarehouseOT getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(WarehouseOT warehouse) {
		this.warehouse = warehouse;
	}

	private Set<DocumentLineOT> lines = new HashSet<DocumentLineOT>();
	
	
	// =new HashSet<DocumentLineOT>();

	public Set<DocumentLineOT> getLines() {
		return lines;
	}

	public void setLines(Set<DocumentLineOT> lines) {
		this.lines = lines;
	}

	public long getIdDocument() {
		return idDocument;
	}

	public void setIdDocument(long idDocument) {
		this.idDocument = idDocument;
	}

	public DocumentTypeOT getDocumentType() {
		return documentType;
	}

	public void setDocumentType(DocumentTypeOT documentType) {
		this.documentType = documentType;
	}

	public Date getDateDocument() {
		return dateDocument;
	}

	public void setDateDocument(Date dateDocument) {
		this.dateDocument = dateDocument;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getSerialNumberPrefix() {
		return serialNumberPrefix;
	}

	public void setSerialNumberPrefix(String serialNumberPrefix) {
		this.serialNumberPrefix = serialNumberPrefix;
	}

	public long getSerialNumberValue() {
		return serialNumberValue;
	}

	public void setSerialNumberValue(long serialNumberValue) {
		this.serialNumberValue = serialNumberValue;
	}

	public PartnerOT getPartner() {
		return partner;
	}

	public void setPartner(PartnerOT partner) {
		this.partner = partner;
	}

	public VendorOT getVendor() {
		return vendor;
	}

	public void setVendor(VendorOT vendor) {
		this.vendor = vendor;
	}

	public CurrencyOT getCurrency() {
		return currency;
	}

	public void setCurrency(CurrencyOT currency) {
		this.currency = currency;
	}

	public PaymentMethodOT getPaymentMethod() {
		return paymentMethod;
	}

	public void setPaymentMethod(PaymentMethodOT paymentMethod) {
		this.paymentMethod = paymentMethod;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public int getVatRegime() {
		return vatRegime;
	}

	public void setVatRegime(int vatRegime) {
		this.vatRegime = vatRegime;
	}

	public BigDecimal getPromptPaymentDiscount() {
		
		
		if (getPaymentMethod() != null)
			if (getPaymentMethod().isCash())
				return promptPaymentDiscount;
		return new BigDecimal(0);
	}

	public void setPromptPaymentDiscount(BigDecimal promptPaymentDiscount) {
		
		
		this.promptPaymentDiscount = promptPaymentDiscount;
	}

	public BigDecimal getFixedDiscount() {
		return fixedDiscount;
	}

	public void setFixedDiscount(BigDecimal fixedDiscount) {
		this.fixedDiscount = fixedDiscount;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getIdNumber() {
		return idNumber;
	}

	public void setIdNumber(String idNumber) {
		this.idNumber = idNumber;
	}

	public String getAddress_billing() {
		return address_billing;
	}

	public void setAddress_billing(String addressBilling) {
		address_billing = addressBilling;
	}

	public String getAddress_shipping() {
		return address_shipping;
	}

	public void setAddress_shipping(String addressShipping) {
		address_shipping = addressShipping;
	}

	public String getPostalCode_billing() {
		return postalCode_billing;
	}

	public void setPostalCode_billing(String postalCodeBilling) {
		postalCode_billing = postalCodeBilling;
	}

	public String getPostalCode_shipping() {
		return postalCode_shipping;
	}

	public void setPostalCode_shipping(String postalCodeShipping) {
		postalCode_shipping = postalCodeShipping;
	}

	public String getCity_billing() {
		return city_billing;
	}

	public void setCity_billing(String cityBilling) {
		city_billing = cityBilling;
	}

	public String getCity_shipping() {
		return city_shipping;
	}

	public void setCity_shipping(String cityShipping) {
		city_shipping = cityShipping;
	}

	public StateOT getState_billing() {
		return state_billing;
	}

	public void setState_billing(StateOT stateBilling) {
		state_billing = stateBilling;
	}

	public StateOT getState_shipping() {
		return state_shipping;
	}

	public void setState_shipping(StateOT stateShipping) {
		state_shipping = stateShipping;
	}

	public CountryOT getCountry_billing() {
		return country_billing;
	}

	public void setCountry_billing(CountryOT countryBilling) {
		country_billing = countryBilling;
	}

	public CountryOT getCountry_shipping() {
		return country_shipping;
	}

	public void setCountry_shipping(CountryOT countryShipping) {
		country_shipping = countryShipping;
	}

	@Override
	public boolean equals(Long id) {
		return id == this.idDocument;
	}

	@Override
	public String getEntityId() {
		return InterfaceManagement.EXPLICA_ENTITY_DOCUMENT_ID;
	}

	@Override
	public long getInternalId() {
		return getIdDocument();
	}

	@Override
	public String getInternalValue() {
		return getSerialNumber();
	}

	public String getSerialNumber() {
		return getSerialNumberPrefix() + "/" + getSerialNumberValue();
	}

	@Override
	public int hashCode() {
		final HashCodeBuilder hcb = new HashCodeBuilder();
		hcb.append(getDocumentType());
		hcb.append(getSerialNumberPrefix());
		hcb.append(getSerialNumberValue());
		return hcb.toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		boolean isEquals = false;
		try {
			final DocumentOT other = (DocumentOT) obj;
			final EqualsBuilder eqb = new EqualsBuilder();
			eqb.append(getDocumentType(), other.getDocumentType());
			eqb.append(getSerialNumberPrefix(), other.getSerialNumberPrefix());
			eqb.append(getSerialNumberValue(), other.getSerialNumberValue());
			isEquals = eqb.isEquals();

		} catch (Exception e) {
			isEquals = false;
		}

		return isEquals;
	}

	public SerialNumberOT getSerial() {
		return serial;
	}

	public void setSerial(SerialNumberOT serial) {
		this.serial = serial;
	}


	
	



	

	

	
	
	
	
	


	
	


	public int nextLineNumber() {
		int result = 0;
		if (getLines() != null){
			int i=0;
			int max=0;
			for (DocumentLineOT l : getLines()){
				if (i==0)
					max=l.getOrder();
				if (l.getOrder()>max)
					max=l.getOrder();
				i++;
			}
			result =max+ InterfaceManagement.EXPLICA_DOCUMENT_LINES_INTERVAL;
		}
		return result;
	}
	public String toString(){
	StringBuffer result = new StringBuffer();
	
	
	
	try {
		result.append("\n%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%%%");
		result.append("\nId Document: ");
		result.append(this.idDocument);
		result.append("\nSerial: ");
		result.append(this.serialNumberPrefix);
		result.append("\nNumber: ");
		result.append(this.serialNumberValue);
		
	}
	catch (Exception ex) {
		System.out.println("Error formatting DocumentOT!");
		ex.printStackTrace();
		return null;
	}
	
	return result.toString();
		
	
	}

	public DocumentTotal getTotal() {
		return total;
	}

	public DocumentOT() {
		
		super();
		total= new DocumentTotal(this);
	}


	

	

	

	

}
